<?php

namespace app\admin\controller;


use app\common\model\Classes;

class Banji extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 分页获取班级信息
     * @return void
     */
    public function list()
    {
        //查询条件
        $where = array();

        if (input('status')!=='') {
            $where['status'] = input('status');
        }
        if (input('name')) {
            $where['name'] = array('like', "%" . input('name') . "%");
        }
        if (input('address')) {
            $where['address'] = array('eq', input('address'));
        }
        if (input('coach')) {
            $where['coach'] = array('eq', input('coach'));
        }

        //分页信息
        $page = max(input('page'), 1);
        $size = 20;
        //获取分页列表
        $list = Classes::getPage($where, $page, $size, 'status desc,id desc');
        //总页码
        $total = Classes::getCount($where);
        //数据返回
        if (!$list) {
            return returnJson(0, '', array('list' => [], 'total' => 0));
        }
        return returnJson(0, '', [
            'list' => $list,
            'size' => $size,
            'total' => $total
        ]);
    }

    /**
     * 班级信息
     * @return \think\response\Json
     */
    public function detail()
    {
        //查询条件
        $where = array('id' => array('eq', input('id')));

        //获取信息
        $detail = Classes::getOne($where);


        //返回信息
        return returnJson(0, '', $detail);
    }


    /**
     * 新增或修改班级
     * @return \think\response\Json|void
     */
    public function edit()
    {
        if (request()->isPost()) {
            $input = input();
            $time = time();
            $data = array(
                'name' => $input['name'],
                'status' => $input['status'],
                'week' => $input['week'],
                'address' => $input['address'],
                'time' => $input['time'],
                'mid' => $input['mid'],
                'coach' => $input['coach'],
                'type' => $input['type'],
                'update_time' => $time,
            );
            if ($input['id']) {
                $data['id'] = $input['id'];
            } else {
                $data['create_time'] = $time;
            }

            if ($data['id']) {
                $res = Classes::update($data, array('id' => $data['id']));
            } else {
                $res = Classes::setInsertGetId($data);
            }
            if ($res) {
                return returnJson(0, '操作成功');
            }
            return returnJson(1, '网络异常，请重试');
        }
    }

    /**
     * 周次班级
     * @return \think\response\Json
     */
    public function classes()
    {
        //查询条件
        $where['status'] = 1;

        if(input('field')){
            $field = input('field');
            $data = Classes::getAll($where,'week asc,time asc',$field);
        }else{
            $field = 'id,name';
            $data = Classes::getColumn($where,$field);
        }


        return returnJson(0, '',$data );

    }

    public function classesweek()
    {
        //查询条件
        $where['status'] = 1;

        $data = Classes::getAll($where,'week asc,time asc');

        $week = array();

        foreach ($data as $k=>$v){
            $week[$v['week']][] = $v;
        }

        return returnJson(0, '',$week );

    }






}
